

*******************************************************************************************************************************************************
*PREDICTING VALUE OF OBSERVABLES:
*** The following code has been modified in order to create two tables:
*		1) a table substituting trimmed income and trimmed profits with winsorized income and winsorized profits as dependent variables
*		2) a table substituting the logarithm of income and the logarithm of profits with the inverse hyperbolic sine transformations of 
*			the variables income and profits as dependent variables.
*******************************************************************************************************************************************************

***********************************************************
*STEP 1- CLEAN CONTROLS FOR LASSO
***********************************************************	
	local controls $controls
		
	*PDS LASSO CODE

		cap drop _*
		cap drop *AA* 
		
		local k=1
		if "`controls'"!="" {
		foreach var of varlist `controls' {
			g AA`k'= `var'
				local ++k
			}
		}

		unab AA: AA*
			lassoClean `AA'
			ds _AA*
		
		foreach var of varlist _AA*{
			sum `var'
			gen miss_`var'=`var'==.
			replace `var'=r(mean) if miss_`var'==1
			gen Winner_`var'=Winner*`var'
			gen mi_Winner_`var'=Winner*miss_`var'
			}


***********************************************************
*STEP 2- RUN PDS LASSO FIR JUST CONTROLS
*grab the controls that pds lasso selects
***********************************************************		
	local m=1
	
*	Uncomment the following line of code (Line 50) to obtain the table substituting the logarithm of income and the logarithm of profits with the inverse hyperbolic 
*	sine transformations of the variables income and profits as dependent variables. 
	foreach out in Trim_Profits_30Days IHS_Profits Trim_Income IHS_Income {	
	    
*	Uncomment the following line of code (Line 53) to obtain the table substituting trimmed income and trimmed profits with winsorized income and winsorized profits as dependent variables
	*foreach out in wins_Profits_30Days log_Profits wins_Income log_Income  {
			
	* run lasso
	pdslasso `out' Winner (Winner__AA* mi_Winner__AA* i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
			
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen MR_Prediction_Obs`m'=_b[Winner]

	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs`m'= MR_Prediction_Obs`m'+_b[`var']*`variable'
	}	
	
	tempvar rank_tercile
		egen `rank_tercile'=cut(MR_Prediction_Obs`m'), group(3)
		tab `rank_tercile', gen(MR_Prediction`m'_Obs_Tercile_)
		gen Winner_MR_Prediction`m'_Obs_T2= Winner*MR_Prediction`m'_Obs_Tercile_2
		gen Winner_MR_Prediction`m'_Obs_T3= Winner*MR_Prediction`m'_Obs_Tercile_3
		

	
	
***********************************************************
*STEP 3- NOW ADD RANK TO PREDICTION
*grab the controls that pds lasso selects
***********************************************************		
	
	* run lasso
	pdslasso `out'  Winner (Winner_Quint_Rank_NS Winner__AA* mi_Winner__AA*   i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
		
		
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen _Quint_Rank_NS`m'=Quint_Rank_NS
	gen MR_Prediction_Obs_Rank`m'=_b[Winner]
	local i=1 
	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs_Rank`m'= MR_Prediction_Obs_Rank`m'+_b[`var']*`variable'
	}	
	
	tempvar rank_tercile
		egen `rank_tercile'=cut(MR_Prediction_Obs_Rank`m'), group(3)
		tab `rank_tercile', gen(MR_Prediction`m'_Obs_Rank_T_)
		gen Winner_MR_Predict`m'_Obs_Rank_T2= Winner*MR_Prediction`m'_Obs_Rank_T_2
		gen Winner_MR_Predict`m'_Obs_Rank_T3= Winner*MR_Prediction`m'_Obs_Rank_T_3	
		

	local m=`m'+1
	}

***********************************************************
*STEP 4- REGRESSION
***********************************************************			
	
*	Uncomment the following command to obtain the table substituting the logarithm of income and the logarithm of profits with the inverse hyperbolic 
*	sine transformations of the variables income and profits as dependent variables
	maketable_t4_2  Trim_Income IHS_Income Trim_Profits_30Days IHS_Profits  if Survey_Version!=5 &  Done==1 & Winner_Quint_Rank_NS!=. , /// 
	table("${outtex}${date}_MR_Predict_Obs_Rank_IHS.tex") ///
	type(tex) /// 
	footnote($footnote_t4_IHS) ///
	prehead1($prehead1) prehead2($prehead2) /// 
	 posthead("\hline") ///
	title("Replicating Table 4 in Hussam et al. (2022b): Using IHS transformation instead of log transformation of outcomes") label(predictobservablesrank_IHS)

	
*	Uncomment the following command to obtain the table substituting trimmed income and trimmed profits with winsorized income and winsorized profits as dependent variables
/*
	maketable_t4_2  wins_Income log_Income wins_Profits_30Days log_Profits if Survey_Version!=5 &  Done==1 & Winner_Quint_Rank_NS!=. , /// 
	table("${outtex}${date}_MR_Predict_Obs_Rank_winsorizing.tex") ///
	type(tex) /// 
	footnote($footnote_t4_winsorizing) ///
	prehead1($prehead1) prehead2($prehead2) /// 
	 posthead("\hline") ///
	title("Replicating Table 4 in Hussam et al. (2022b): Winsorizing instead of Trimming Outcomes") label(predictobservablesrank_winsorizing)
/*


